import Packet from "../net/Packet"
import GoodsItem from './GoodsItem'

// 物品列表
export default class GoodsList {
	/**
	 * @param goodsItems 物品列表
	 */
	public constructor(public goodsItems: GoodsItem[]) { }

	public encode(): Packet {
		const packet = this._encode()
		packet.Encode(2020)
		return packet
	}

	public encodeMsg(): ByteBuffer {
		return this._encode().GetBuffer()
	}

	public static decode(packet: Packet): GoodsList {
		const goodsItems = []
		const goodsItemsCount = packet.ReadUshort()
		for (let i = 0; i < goodsItemsCount; i++) {
			goodsItems.push(GoodsItem.decode(packet))
		}
		return new GoodsList(goodsItems)
	}

	private _encode(): Packet {
		const packet = new Packet()
		const goodsItemsCount = this.goodsItems.length
		packet.WriteUshort(goodsItemsCount)
		for (let i = 0; i < goodsItemsCount; i++) {
			const xxx = this.goodsItems[i]
			packet.WriteBuffer(xxx.encodeMsg())
		}
		return packet
	}
}
